package com.nekoneko.nekonekodemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.*;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.os.Environment.DIRECTORY_DOWNLOADS;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Handler handler;
    long delayTime = 1000;
    int what = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("handleMessage", " Looper.prepare()");
                Looper.prepare();
                handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        Log.d("handleMessage", "" + Thread.currentThread().getName());
                    }
                };
                Looper.loop();
                Log.d("handleMessage", " Looper.loop()");
            }
        }).start();

        int a = 1;
        int b = a;
        a = 0;
        Log.d("b", "b=" + b);
    }


    @OnClick({R.id.processView, R.id.swipeMenuView, R.id.video_call_fragment, R.id.camera_fragment,
            R.id.album, R.id.settings, R.id.download, R.id.video_fragment, R.id.wheel_fragment, R.id.drawable_fragment, R.id.touchEvent,
            R.id.handler, R.id.overScroll})
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.handler) {
            delayTime = delayTime + 1000;
            Message message = new Message();
            Log.d("message", "message beforeSend=" + message.getWhen());
            message.what = what++;
            handler.sendMessageDelayed(message, delayTime);
            int w = message.what;
            Log.d("message", "message what=" + what + " when=" + message.getWhen());
            return;
        }
        if (view.getId() == R.id.download) {
            String url = "http://dl-cdn.coolapkmarket.com/down/apk_file/2017/0818/com.coolapk.market-7.9.7-1708181.apk?_upt=a95adf371512464075&chinly_hou=c";
            downLoad(url, this);
            return;
        }
        if (view.getId() == R.id.touchEvent) {
            startActivity(new Intent(this, TouchActivity.class));
            return;
        }
        if (view.getId() == R.id.album) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            AlertDialog alertDialog = builder.setView(R.layout.dialog_photo_video).create();
            alertDialog.show();
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = null;
                    switch (view.getId()) {
                        case R.id.tv_from_album:
                            uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                            break;
                        case R.id.tv_from_video:
                            uri = android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                            break;
                    }
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_PICK);
                    intent.setData(uri);
                    startActivityForResult(intent, 1);
                }
            };
            alertDialog.findViewById(R.id.tv_from_album).setOnClickListener(clickListener);
            alertDialog.findViewById(R.id.tv_from_video).setOnClickListener(clickListener);
            return;
        }
        if (view.getId() == R.id.settings) {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
            return;
        }
        Intent intent = new Intent(this, DemoActivity.class);
        intent.putExtra("action", view.getId());
        startActivity(intent);
    }

    private void downLoad(final String url, final Context context) {
        final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(R.layout.dialog_downloading).create();
        alertDialog.show();
        alertDialog.setCancelable(false);
        final ProgressBar progressBar = alertDialog.findViewById(R.id.processView);
        final TextView tvProcess = alertDialog.findViewById(R.id.tv_process);
        new Thread() {
            @Override
            public void run() {
                super.run();
                String dir = getExternalFilesDir(DIRECTORY_DOWNLOADS).getPath();
                String fileName = System.currentTimeMillis() + ".apk";
                final String path = dir + File.separator + fileName;
                DownloadUtils.downLoadFromUrl(url, fileName, dir, new DownloadUtils.DownloadCallBack() {
                    @Override
                    public void onProcess(int process) {
                        Log.d("downLoad", "process=" + process);
                        progressBar.setProgress(process);
                        tvProcess.setText(process + "%");
                    }

                    @Override
                    public void onDone() {
                        Log.d("downLoad", "onDone");
                        alertDialog.dismiss();
                        Toast.makeText(context, "下载成功。", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                        intent.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
                        Activity activity = (Activity) context;
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onFailed() {
                        Toast.makeText(context, "下载失败。", Toast.LENGTH_SHORT).show();
                        Log.d("downLoad", "onFailed");
                        alertDialog.dismiss();
                    }
                });
            }
        }.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public static class MsgBean {

    }

}
